<?php session_start(); ?>
<?php include_once('model/country.php'); ?>
<?php include_once('misc/class_config.php'); ?>
<?php
	include_once('access_control/page_access.php');
	setPreviousPage();
	declinePageIfNotLoggedIn();
	declinePageIfNotAdmin();
	$currentUser= new user();
	$currentUser->isLoggedIn();
	$currentUser->isTimedOut();
?>


<?php
	$unknownUser = new user();
	$registerMessage = '';
	
	$firstNameErrorMessage = '';
	$lastNameErrorMessage = '';
	$address1ErrorMessage = '';
	$address2ErrorMessage = '';
	$countryErrorMessage = '';
	$dobErrorMessage = '';
	$emailAddressErrorMessage = '';
	$passwordErrorMessage = '';
	
	if(isset($_POST['btnRegister']))
	{
		$registerValidationError = new validation();
		
		$firstNameErrorMessage = $registerValidationError->isEmpty(trim($_POST['first_name']),'<label class="required">First name is required.</label>');
		$lastNameErrorMessage = $registerValidationError->isEmpty(trim($_POST['last_name']),'<label class="required"> Last name required.</label>');;
		$address1ErrorMessage = $registerValidationError->isEmpty(trim($_POST['address1']),'<label class="required">Address address is required.</label>');
		$address2ErrorMessage = $registerValidationError->isEmpty(trim($_POST['address2']),'<label class="required">Address address is required.</label>');
		$countryErrorMessage = $registerValidationError->isListItemSelected(trim($_POST['country']),'<label class="required">Select a Counrty.</label>');
		$dobErrorMessage = $registerValidationError->isEmpty(trim($_POST['dob']),'<label class="required">DOB is required.</label>');
		$emailAddressErrorMessage = $registerValidationError->isEmpty(trim($_POST['email_address']),'<label class="required">Email address is required.</label>');
		$passwordErrorMessage = $registerValidationError->isEmpty(trim($_POST['password']),'<label class="required">Password address is required.</label>');
		
		$unknownUser->setUser('',$_POST['first_name'],$_POST['last_name'],$_POST['address1'],$_POST['address2'],$_POST['country'],$_POST['dob'],'admin',$_POST['password'],$_POST['email_address'],'');
	}
?>

<?php
	
	if(isset($_POST['btnRegister']))
	{
		$formError = $registerValidationError->status;
		
		if($formError==0)
		{
			try
			{
				if($unknownUser->registerUser())
				{
					$registerMessage = "registered in";
				}
				else
				{
					$registerMessage = 'An error occured';
				}
			}
			catch(Exception $ex)
			{
				$registerMessage = $ex->getMessage();
			}
		}
	}
?>

<?php $pageTitle = "Add Administrator| Everything"; ?>
<?php include('layout/page_start.php'); ?>
<?php include('layout/page_header.php'); ?>
<?php include('layout/menu_bar.php'); ?>

<div id="main_content_div">
	<?php include("layout/configuration_menu_bar.php"); ?>
	<div id="register_form_div" class="form_div">
		<h2>Add Administrator</h2>
		<form action="adduser.php" method="post">
			<table>
				<tr>
					<td><label>First Name:</label></td> <td><input type="text" name="first_name" value="<?php echo $unknownUser->firstName; ?>"/>&nbsp &nbsp <?php echo $firstNameErrorMessage; ?></td>
				</tr>
				<tr>
					<td><label>LastName:</label></td> <td><input type="text" name="last_name" value="<?php echo $unknownUser->lastName; ?>"/>&nbsp &nbsp <?php echo $lastNameErrorMessage; ?></td>
				</tr>
				<tr>
					<td><label>DOB (<strong>yyyy/mm/dd</strong>):</label></td> <td><input type="text" value="<?php echo $unknownUser->dob; ?>" name="dob"><input type="button" value="Calendar" onclick="displayCalendar(document.forms[0].dob,'yyyy/mm/dd',this)">&nbsp &nbsp <?php echo $dobErrorMessage; ?></td>
				</tr>
				<tr>
					<td><label>Address 1:</label></td> <td><input type="text" name="address1" value="<?php echo $unknownUser->address1; ?>"/>&nbsp &nbsp <?php echo $address1ErrorMessage; ?></td>
				</tr>
				<tr>
					<td><label>Address 2:</label></td> <td><input type="text" name="address2" value="<?php echo $unknownUser->address2; ?>"/>&nbsp &nbsp  <?php echo $address2ErrorMessage; ?></td>
				</tr>
				<tr>
					<td><label>Country:</label></td>
					<td><select name="country">
						<option value="0" selected>Select your country</option>
						<?php
							try
							{	
								$countrySet = getAllCountry();
								
								if($countrySet)
								{
									
									foreach($countrySet as $countryRecord)
									{
										echo '<option value="'.$countryRecord['code'].'">'.$countryRecord['name'].'</option>';
									}
									
								}
							}
							catch(Exception $ex)
							{
								
								$registerMessage = $ex->getMesssage();
							}
							
						?>
					</select>&nbsp &nbsp <?php echo $countryErrorMessage; ?></td> 
				</tr>
				<tr>
					<td><label>Email Address:</label></td> <td><input type="text" name="email_address" value="<?php echo $unknownUser->emailAddress; ?>"/>&nbsp &nbsp <?php echo $emailAddressErrorMessage; ?></td>
				</tr>
				<tr>
					<td><label>Password:</label></td> <td><input type="password" name="password"/>&nbsp &nbsp <?php echo $passwordErrorMessage; ?></td>
				</tr>
			</table>
			<div class="form_button_div">
				<input type="submit" name="btnRegister" value="Register"/> &nbsp &nbsp &nbsp 
				<input type="reset" name="btnReset" value="Reset Fields"/>
			</div>

			<label><?php if(isset($registerMessage)){echo $registerMessage;} ?></label>
		</form>
	</div></br>
</div>
<?php include('layout/page_footer.php'); ?>
<?php include('layout/page_end.php'); ?>
<?php setCurrentPage();?>